package com.sname.car.common;

import com.google.gson.Gson;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.sname.car.common.constants.Constants;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author songx
 * @date 2018/9/25 15:23
 */
public class QiNiuFileUploadUtil {
    /**
     * Author song  醉美柳舞之众星捧月
     * Date & Time  2018/9/25 15:25
     * Description  七牛云上传文件 Api
     */
    public static String uploadImg(MultipartFile file) throws IOException {
        Configuration configuration = new Configuration(Zone.zone2());
        UploadManager uploadManager = new UploadManager(configuration);
        Auth auth = Auth.create(Constants.QINIU_ACCESS_KEY, Constants.QINIU_SECRET_KEY);
        String uploadToken = auth.uploadToken(Constants.QINIU_HEAD_IMG_BUCKET_NAME);
        Response response = uploadManager.put(file.getBytes(), null, uploadToken);
        /** 解析上传成功的结果 **/
        DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
        return putRet.key;
    }
}
